﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;

/// <summary>
/// Description résumée de SiteService
/// </summary>
/// 
namespace BLL
{
    public class SiteService
    {
        private ISiteManager siteManager;
        private IActiviteManager activiteManager;
        private CreneauService creneauService;

        public SiteService(ISiteManager siteManager, IActiviteManager activiteManager, CreneauService creneauService)
        {
            this.siteManager = siteManager;
            this.activiteManager = activiteManager;
            this.creneauService = creneauService;
        }

        public SITE createSite(int zoneId, string nom, double latitude, double longitude)
        {
            SITE site = new SITE();
            site.ZONEID = zoneId;
            site.NOM = nom;
            site.LATITUDE = latitude;
            site.LONGITUDE = longitude;

            try
            {
                this.siteManager.addSite(site);
                return site;
            }

            catch (Exception ex)
            {
                return null;
            }

        }

        public bool deleteSite(int siteId)
        {
            return siteManager.deleteSite(siteId);
        }


        public bool updateSite(int id, string nom, double latitude, double longitude, int zoneId)
        {
            try
            {
                SITE site = siteManager.getSiteById(id);
                site.NOM = nom;
                site.LATITUDE = latitude;
                site.LONGITUDE = longitude;
                site.ZONEID = zoneId;

                siteManager.updateSite();

                return true;
            }

            catch (Exception ex)
            {
                return false;
            }
        }

        public SITE getSiteById(int id)
        {
            return siteManager.getSiteById(id);
        }

        public List<SITE> getAllSites()
        {
            return siteManager.getAllSites();
        }

        public string getNom(int id)
        {
            return this.siteManager.getNom(id);
        }

        public List<SITE> getSitesForProgrammeAndOrderAtDate(int programmeId, int ordre, DateTime date)
        {
            List<SITE> sites = siteManager.getAllSites();
            List<CRENEAU> creneaux = creneauService.getCreneauxAtDateAndOrder(date, ordre);

            CRENEAU creneauCourant = creneauService.getCreneauForProgrammeAndOrder(programmeId, ordre);

            CRENEAU crePrecedent = null;
            if (ordre > 1)
                crePrecedent = creneauService.getCreneauForProgrammeAndOrder(programmeId, ordre - 1);

            CRENEAU creSuivant = null;
            if (ordre < 5)
                creSuivant = creneauService.getCreneauForProgrammeAndOrder(programmeId, ordre + 1);

            List<SITE> res = new List<SITE>();

            foreach (SITE site in sites)
            {
                bool creneauLibre = true;

                foreach (CRENEAU creneau in creneaux)
                {
                    if (creneau.SITEID == site.ID)
                    {
                        if (creneau.PROGRAMME.NAVIRE.CATEGORIE.NOM != "Yacht")
                        {
                            creneauLibre = false;
                            break;
                        }
                    }
                }

                if (creneauLibre && (crePrecedent == null || crePrecedent.SITEID != site.ID) && (creSuivant == null || creSuivant.SITEID != site.ID))
                {
                    res.Add(site);
                }
                else
                {
                    if (creneauCourant != null && creneauCourant.SITEID == site.ID)
                    {
                        res.Add(site);
                    }
                }
            }

            return res;
        }

    }
}